package com.block.source.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.vo.ResultInfoVO;
import com.block.db.entity.SourceBaseInfo;
import com.block.db.entity.SourceRecordInfo;
import com.block.source.service.ISourceQueryService;
import com.block.source.service.ISourceRecordInfoService;
import com.block.test.reqbean.source.QueryParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SourceQueryServiceImpl implements ISourceQueryService {

    @Resource
    ISourceRecordInfoService sourceRecordInfoService;

    @Override
    public ResultInfoVO querySourceResult(SourceBaseInfo sourceBaseInfo, QueryParam queryParam) {

        SourceRecordInfo sourceRecordInfo = sourceRecordInfoService.getById(queryParam.getTradeNo());
        if (ObjectUtil.isEmpty(sourceRecordInfo)){
            return ResultInfoVO.error("订单信息为空");
        }
        if (!StrUtil.equals(queryParam.getMobileMd5(), sourceRecordInfo.getMobileMd5())){
            return ResultInfoVO.error("订单的手机号md5与参数不符合");
        }

        JSONObject returnObject = new JSONObject();
        if (StrUtil.equals("1", String.valueOf(sourceRecordInfo.getStatus()))){
            // 推送成功
            String verifyInfo = StrUtil.format("{}:{}", sourceRecordInfo.getUid(), sourceRecordInfo.getSpeInfoId());
            returnObject.put("settleFlag", "1");
            returnObject.put("verifyInfo", verifyInfo);
        }else if (StrUtil.equals("3", String.valueOf(sourceRecordInfo.getStatus()))){
            returnObject.put("settleFlag", "1");
            returnObject.put("message", "测试数据不结算");
        }else {
            returnObject.put("settleFlag", "0");
        }

        return ResultInfoVO.success(returnObject);
    }
}
